package cn.yujian95.hospital.controller;


import cn.yujian95.hospital.common.api.CommonResult;
import cn.yujian95.hospital.dto.param.HospitalProductParam;
import cn.yujian95.hospital.entity.HospitalProduct;
import cn.yujian95.hospital.service.IHospitalProductService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(value = "产品模块", tags = "医院产品接口")
@RestController
@RequestMapping("/hospital")
public class HospitalProductController {

    @Resource
    private IHospitalProductService hospitalProductService;

    @ApiOperation(value = "添加产品", notes = "传入 产品的对象")
    @RequestMapping(value = "/product", method = RequestMethod.POST)
    public CommonResult insertProduct(@RequestBody HospitalProductParam param) {

        String name = param.getName();
        String price = param.getPrice();

        if (ObjectUtils.isEmpty(name) || ObjectUtils.isEmpty(price)) {
            return CommonResult.failed("请填写名称或上传图片或价格");
        }

        if (hospitalProductService.count(name)) {
            return CommonResult.validateFailed("产品名称已存在！");
        }

        if (hospitalProductService.insertProduct(param)) {
            return CommonResult.success();
        }


        return CommonResult.failed("服务器错误，请联系管理员！");
    }

    @ApiOperation(value = "更新产品信息", notes = "传入 产品编号、产品信息参数（名称，描述）")
    @ApiImplicitParam(name = "id", value = "产品编号", paramType = "path", dataType = "Long", required = true)
    @RequestMapping(value = "/product/{id}", method = RequestMethod.PUT)
    public CommonResult updateProduct(@PathVariable Long id, @RequestBody HospitalProductParam param) {

        String name = param.getName();
        String price = param.getPrice();

        if (ObjectUtils.isEmpty(name) || ObjectUtils.isEmpty(price)) {
            return CommonResult.failed("请填写名称或价格");
        }

        if (!hospitalProductService.count(id)) {
            return CommonResult.validateFailed("不存在，该产品! ");
        }

        if (hospitalProductService.update(id, param)) {
            return CommonResult.success();
        }

        return CommonResult.failed("服务器错误，请联系管理员！");
    }

    @ApiOperation(value = "删除产品信息", notes = "传入 产品编号")
    @ApiImplicitParam(name = "id", value = "产品编号", paramType = "path", dataType = "Long", required = true)
    @RequestMapping(value = "/product/{id}", method = RequestMethod.DELETE)
    public CommonResult deleteProduct(@PathVariable Long id) {

        if (!hospitalProductService.count(id)) {
            return CommonResult.validateFailed("不存在，该产品编号! ");
        }

        if (hospitalProductService.delete(id)) {
            return CommonResult.success();
        }
        return CommonResult.failed("服务器错误，请联系管理员！");
    }

    @ApiOperation(value = "获取产品", notes = "传入 产品编号")
    @ApiImplicitParam(name = "id", value = "产品编号", paramType = "path", dataType = "Long", required = true)
    @RequestMapping(value = "/product/{id}", method = RequestMethod.GET)
    public CommonResult<HospitalProduct> getProduct(@PathVariable Long id) {

        if (!hospitalProductService.count(id)) {
            return CommonResult.validateFailed("不存在，该产品编号! ");
        }

        HospitalProduct optionalProduct = hospitalProductService.getProduct(id);

        return CommonResult.success(optionalProduct);
    }


    @ApiOperation(value = "分页：搜索产品信息", notes = "传入 产品名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "产品名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "第几页", paramType = "query", dataType = "Integer",
                    required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query", dataType = "Integer",
                    required = true),
    })
    @RequestMapping(value = "/product/list", method = RequestMethod.GET)
    public CommonResult<IPage<HospitalProduct>> searchProduct(@RequestParam(required = false) String name,
                                                              @RequestParam Integer pageNum,
                                                              @RequestParam Integer pageSize) {

        IPage<HospitalProduct> list = hospitalProductService.list(name, pageNum, pageSize);

        return CommonResult.success(list);
    }

    @ApiOperation(value = "分页：搜索产品信息(小程序端)判断上架，并且在时间段内", notes = "传入 产品名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "产品名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "第几页", paramType = "query", dataType = "Integer",
                    required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query", dataType = "Integer",
                    required = true),
    })
    @RequestMapping(value = "/product/app/list", method = RequestMethod.GET)
    public CommonResult<IPage<HospitalProduct>> searchProductByApp(@RequestParam(required = false) String name,
                                                                   @RequestParam Integer pageNum,
                                                                   @RequestParam Integer pageSize) {

        return CommonResult.success(hospitalProductService.listForApp(name, pageNum, pageSize));
    }


    @ApiOperation(value = "分页：搜索全部产品信息(小程序端)", notes = "传入 产品名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "第几页", paramType = "query", dataType = "Integer",
                    required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query", dataType = "Integer",
                    required = true),
    })
    @RequestMapping(value = "/product/app/searchList", method = RequestMethod.GET)
    public CommonResult<IPage<HospitalProduct>> searchProductListByApp(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize) {

        return CommonResult.success(hospitalProductService.listForAppList(pageNum, pageSize));
    }


}
